/**
 * Group table.
 */
var groupTable = {};
var prefix = "o:r:";
var s_key = "o:r:s";
var prefix_m = "o:r:m:";

function gettime() {
	"use strict";
	var d = new Date();
	return d.toUTCString();
}

function defaultCallback(err) {
	if (err) {
		console.error("groupTable redis error:" + err);
	}
}

groupTable.add = function(context, group, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	console.error("add new group:");
	console.error(group);
	
	var key = prefix + group.id;
	
	var fields = [];
	fields.push(key);
	fields.push("id");
	fields.push(group.id);
	fields.push("name");
	fields.push(group.name);
	fields.push("type");
	fields.push(group.type);
	fields.push("gwId");
	fields.push(group.gwId);
	fields.push("description");
	fields.push(group.description);
	fields.push("createtime");
	fields.push(gettime());
	fields.push("tindex");
	fields.push("");

	context.beginMulti();
	redis.sadd(s_key, group.id, defaultCallback);
	redis.hmset(fields, callback);
	context.endMulti(defaultCallback);
};

groupTable.loadFields = function(context, id, fields, callback) {
	"use strict";
	var redis = context.redis;
	
	var key = prefix + id;
	redis.hmget(key, fields, callback);
};

groupTable.setFields = function(context, id, fields, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	var fields2 = [key];
	fields2 = fields2.concat(fields);
	redis.hmset(fields2, callback);
};

groupTable.del = function(context, id, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var key = prefix + id;
	var keym = prefix_m + id;
	context.beginMulti();
	redis.srem(s_key, id, defaultCallback);
	redis.del(keym, defaultCallback);
	redis.del(key, callback);
	context.endMulti(defaultCallback);
};

groupTable.scan = function(context, cursor, count, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.sscan([s_key, cursor, "COUNT", count], callback);
};

groupTable.count = function(context, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	redis.scard(s_key, callback);
};

groupTable.add_m = function(context, groupId, controllerId, idx, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = idx+"@"+controllerId;
	var key = prefix_m + groupId;
	redis.sadd(key, field_name, callback);
};

groupTable.del_m = function(context, groupId, controllerId, index, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}
	
	var field_name = index + "@" + controllerId;
	var key = prefix_m + groupId;
	redis.srem(key, field_name, callback);
};

groupTable.getAll_m = function(context, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_m + groupId;
	redis.smembers(key, callback);
};

groupTable.count_m = function(context, groupId, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_m + groupId;
	redis.scard(key, callback);
};

groupTable.exist_m = function(context, groupId, controllerId, index, callback) {
	"use strict";
	var redis = context.redis;
	if (!callback) {
		callback = defaultCallback;
	}

	var key = prefix_m + groupId;
	var field_name = index+"@"+controllerId;
	
	redis.sismember(key, field_name, callback);
};

module.exports = groupTable;
